import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/member.dart';
import '../providers/member_provider.dart';
import './edit_member_dialog.dart';
import './password_dialog.dart';

class MemberItem extends StatelessWidget {
  final Member member;

  const MemberItem({
    super.key,
    required this.member,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: ListTile(
        leading: CircleAvatar(
          child: Text(member.name[0]),
        ),
        title: Text(member.name),
        subtitle: Text(member.email),
        trailing: PopupMenuButton(
          itemBuilder: (context) => [
            PopupMenuItem(
              value: 'edit',
              child: Row(
                children: const [
                  Icon(Icons.edit),
                  SizedBox(width: 8),
                  Text('编辑'),
                ],
              ),
            ),
            PopupMenuItem(
              value: 'delete',
              child: Row(
                children: const [
                  Icon(Icons.delete),
                  SizedBox(width: 8),
                  Text('删除'),
                ],
              ),
            ),
            PopupMenuItem(
              value: 'password',
              child: Row(
                children: const [
                  Icon(Icons.lock),
                  SizedBox(width: 8),
                  Text('密码管理'),
                ],
              ),
            ),
          ],
          onSelected: (value) {
            switch (value) {
              case 'edit':
                _editMember(context);
                break;
              case 'delete':
                _deleteMember(context);
                break;
              case 'password':
                _managePassword(context);
                break;
            }
          },
        ),
      ),
    );
  }

  void _editMember(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => EditMemberDialog(member: member),
    );
  }

  void _deleteMember(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('删除成员'),
        content: Text('确定要删除成员 ${member.name} 吗？'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () {
              final memberProvider = Provider.of<MemberProvider>(context, listen: false);
              memberProvider.deleteMember(member.id).then((_) {
                Navigator.pop(context);
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('成员 ${member.name} 已删除')),
                );
              }).catchError((error) {
                Navigator.pop(context);
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('删除成员失败: $error')),
                );
              });
            },
            child: const Text(
              '删除',
              style: TextStyle(color: Colors.red),
            ),
          ),
        ],
      ),
    );
  }

  void _managePassword(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => PasswordDialog(member: member),
    );
  }
} 